package com.mpush.mpns.web.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mpush.common.user.UserManager;
import com.mpush.mpns.biz.domain.NotifyDO;
import com.mpush.mpns.biz.service.MPushManager;
import com.mpush.mpns.biz.service.PushService;
import com.mpush.mpns.web.common.ApiResult;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.eventbus.Message;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by yxx on 2016/4/26.
 *
 * @author ohun@live.cn
 */
@Controller
public class AdminHandler extends BaseHandler {

    @Resource
    private PushService pushService;

    @Resource
    private MPushManager mPushManager;

    @Override
    public String getRootPath() {
        return "/admin";
    }

    @Override
    protected void initRouter(Router router) {
        //推送指定用户
        router("/push", this::sendPush);
        //推送用户组
        router("/pushList", this::sendPushList);
        //查询mpushServer列表
        router("/list/servers", this::listMPushServers);
        //查询mpushServer在线用户数
        router("/get/onlineUserNum", this::getOnlineUserNum);
        //推送特定条件用户
        router("/pushTags", this::sendPushTags);
        //踢出用户
        router("/kickUser", this::kickUser);
        //查询在线用户列表
        router("/get/onlineUserList", this::getOnlineUserList);
        initConsumer(eventBus);
        //推送用户自定义条件
        router("/pushExtra", this::sendPushExtra);
    }

    protected void initConsumer(EventBus eventBus) {
        consumer("/getUser", this::onTestEvent);
    }

    public void listMPushServers(RoutingContext rc) {
        rc.response().end(new ApiResult<>(mPushManager.getConnectServerList()).toString());
    }

    public void getOnlineUserNum(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String ip = null;
        if((String)pushInfoObj.get("ip") != null && !"".equals((String)pushInfoObj.get("ip"))){
            ip = pushInfoObj.getString("ip");
        }
        //ip = rc.request().getParam("ip");
        rc.response().end(new ApiResult<>(mPushManager.getOnlineUserNum(ip)).toString());
    }

    public void sendPush(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String userId = null;
        if((String)pushInfoObj.get("userId") != null && !"".equals((String)pushInfoObj.get("userId"))){
            userId = pushInfoObj.getString("userId");
        }
        String content = null;
        if((String)pushInfoObj.get("content") != null && !"".equals((String)pushInfoObj.get("content"))){
            content = pushInfoObj.getString("content");
        }
        boolean success = pushService.notify(userId, new NotifyDO(content));
        rc.response().end(new ApiResult<>(success).toString());
    }

    public void sendPushList(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String userList = null;
        if((String)pushInfoObj.get("userIds") != null && !"".equals((String)pushInfoObj.get("userIds"))){
            userList = pushInfoObj.getString("userIds");
        }
        String content = null;
        if((String)pushInfoObj.get("content") != null && !"".equals((String)pushInfoObj.get("content"))){
            content = pushInfoObj.getString("content");
        }
        boolean success = pushService.notifyList(userList, new NotifyDO(content));
        rc.response().end(new ApiResult<>(success).toString());
    }

    public void sendPushTags(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String condition = null;
        if(pushInfoObj.getString("condition") != null && !"".equals(pushInfoObj.getString("condition"))){
            condition = pushInfoObj.getString("condition");
        }
        String content = null;
        if((String)pushInfoObj.get("content") != null && !"".equals((String)pushInfoObj.get("content"))){
            content = pushInfoObj.getString("content");
        }
        boolean success = pushService.notifyTags(condition, new NotifyDO(content));
        rc.response().end(new ApiResult<>(success).toString());
    }

    public void kickUser(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String userId = null;
        if((String)pushInfoObj.get("userId") != null && !"".equals((String)pushInfoObj.get("userId"))){
            userId = pushInfoObj.getString("userId");
        }
        try{
            mPushManager.kickUser(userId);
            rc.response().end(new ApiResult<>(true).toString());
        }catch(Exception e){
            rc.response().end(new ApiResult<>(false).toString());
            e.printStackTrace();
        }
    }

    public void getOnlineUserList(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String ip = null;
        if((String)pushInfoObj.get("ip") != null && !"".equals((String)pushInfoObj.get("ip"))){
            ip = pushInfoObj.getString("ip");
        }
        int start = 0;
        if((String)pushInfoObj.get("start") != null && !"".equals((String)pushInfoObj.get("start"))){
            try {
                start = Integer.parseInt(pushInfoObj.getString("start"));
            } catch (Exception e) {
                rc.response().end(new ApiResult<>(500, "please enter integral type").toString());
            }
        }
        int end = 0;
        if((String)pushInfoObj.get("end") != null && !"".equals((String)pushInfoObj.get("end"))){
            try {
                end = Integer.parseInt(pushInfoObj.getString("end"));
            } catch (Exception e) {
                rc.response().end(new ApiResult<>(500,  "please enter integral type").toString());
            }
        }
        rc.response().end(new ApiResult<>(mPushManager.getOnlineUserList(ip,start,end)).toString());
    }

    public void sendPushExtra(RoutingContext rc) {
        String pushInfo = rc.request().getParam("pushInfo");
        net.sf.json.JSONObject pushInfoObj=net.sf.json.JSONObject.fromObject(pushInfo);
        String extra = null;
        if(pushInfoObj.getString("extra") != null && !"".equals(pushInfoObj.getString("extra"))){
            extra = pushInfoObj.getString("extra");
        }
        String content = null;
        if((String)pushInfoObj.get("content") != null && !"".equals((String)pushInfoObj.get("content"))){
            content = pushInfoObj.getString("content");
        }
        boolean success = pushService.notifyExtra(extra, new NotifyDO(content));
        rc.response().end(new ApiResult<>(success).toString());
    }

    public void onTestEvent(Message<JsonObject> event) {
        JsonObject object = event.body();
        object.put("data", "server replay msg");
        event.reply(object);
    }
}
